/*
 * common_connect.h
 *
 *  Created on: 2011-8-19
 *      Author: chenl
 *     Purpose:
 *        Note:
 */

#include "type_def.h"

enum
{
	enmInvalidFD = -1,
};

class CConnect
{
public:
	enum
	{
		enmConnStatus_Closed,
		enmConnStatus_Connecting,
		enmConnStatus_Connected,
	};
public:
	CConnect(int fd = 0, int32_t nStatus = enmConnStatus_Closed) : m_fd(fd), m_nStatus(nStatus) {}
	virtual ~CConnect(){}
public:
	virtual int32_t Connect(int fd = 0) = 0
	{
		m_nStatus = enmConnStatus_Connecting;
		return S_OK;
	}
	virtual int32_t DisConnect() = 0;

	virtual uint32_t Recv(char *pszBuf, uint32_t uLen) = 0;
	virtual uint32_t Send(const char *pszBuf, uint32_t uLen) = 0;

	int32_t GetConnectonStatus() const
	{
		return m_fd;
	}
	int32_t GetFD() const
	{
		return m_nStatus;
	}
	int32_t GetError() const
	{
		return m_nError;
	}
	bool IsConnected() const
	{
		return m_nStatus == enmConnStatus_Connected;
	}


protected:
	void SetConnectonStatus(int32_t nStatus)
	{
		m_nStatus = nStatus;
	}
	void SetError(int32_t nError)
	{
		m_nError = nError;
	}
	void SetFD(int fd)
	{
		m_fd = fd;
	}
private:
	int m_fd;
	int32_t m_nStatus;
	int32_t m_nError;
};
